<?php
namespace app\admin\controller;
use app\common\model\User as UserModel;
use app\common\model\Grade as GradeModel;
use app\common\model\Mall as MallModel;
use app\common\model\ActionLog as ActionLogModel;
use app\service\ProfessionalService;

class User extends Base
{   
    public $userModel;
    public function __construct () {
        parent::__construct();
        $this->userModel = new UserModel;
    }

    /**
     * 职称列表
     */
    public function professional()
    {
        $data = input('');

        $where = [];
        isset($data['name']) && !empty($data['name']) && $where['name'] = array('like', '%' . $data['name'] . '%');

        $return_data = ProfessionalService::S()->queryList($where, 'id,name');
        $return_count = ProfessionalService::S()->count($where);

        return returnJson(200, 'success', [
            'list' => $return_data,
            'total' => $return_count
        ]);
    }

    /**
     * 添加职称
     * @return \think\response\Json
     */
    public function addProfessional()
    {
        $data = input('');
        if (empty($data['name'])) {
            return returnJson(202, '请填写职称名称');
        } else {
            $professional = ProfessionalService::S()->find(['name' => ['eq', $data['name']]]);
            if ($professional) {
                return returnJson(202, '该职称已存在');
            }
        }
        $save_data = [
            'name' => $data['name']
        ];
        $res = ProfessionalService::S()->add($save_data);
        if ($res) {
            return returnJson(200, '添加成功', $res);
        } else {
            return returnJson(202, '添加失败');
        }

    }

    /**
     * 编辑职称
     * @return \think\response\Json
     */
    public function editProfessional()
    {
        $data = input('');
        if (empty($data['id'])) {
            return returnJson(202, '参数错误');
        } else {
            $professional = ProfessionalService::S()->find(['id' => ['eq', $data['id']]]);
            if (!$professional)
                return returnJson(202, '职称信息不存在');
        }
        if (empty($data['name'])) {
            return returnJson(202, '请填写职称名称');
        } else {
            $professional = ProfessionalService::S()->find(['name' => ['eq', $data['name']]]);
            if ($professional) {
                return returnJson(202, '该职称已存在');
            }
        }
        $save_data = [
            'name' => $data['name']
        ];
        $res = ProfessionalService::S()->save($save_data, $data['id']);
        if ($res) {
            return returnJson(200, '编辑成功', $res);
        } else {
            return returnJson(202, '编辑失败');
        }

    }

    /**
     * 删除职称
     * @return \think\response\Json
     */
    public function delProfessional()
    {
        $data = input('');
        if (empty($data['id'])) {
            return returnJson(202, '参数错误');
        } else {
            $professional = ProfessionalService::S()->find(['id' => ['eq', $data['id']]]);
            if (!$professional)
                return returnJson(202, '职称信息不存在');
        }
        $res = ProfessionalService::S()->del( $data['id']);
        if ($res) {
            return returnJson(200, '删除成功', $res);
        } else {
            return returnJson(202, '删除失败');
        }

    }

    /* 
     列表
     */
    public function list()
    {
        $where = array();
        $input = input();
        if($input['id']) {
            $where['id'] = $input['id'];
        }
        if($input['refer_id']) {
            $where['refer_id'] = $input['refer_id'];
        }
        if(isset($input['chat_forbidden'])&&$input['chat_forbidden']!=='') {
            $where['chat_forbidden'] = $input['chat_forbidden'];
        }
        if($input['name']) {
            $where['nickname|realname'] = [
                'like',"%{$input['name']}%"
            ];
        }
        if($input['grade_id']>0) {
            $where['grade_id'] = $input['grade_id'];
        }
        if($this->manager['mall_id']==0) {
            if($input['mall_id']) {
                $where['mall_id'] = $input['mall_id'];
            }
        }else{
            $where['mall_id'] = $this->manager['mall_id'];
        }
        
        $page = max(input('page'),1);
        $size = 20;
        $userList = $this->userModel->getPage($where,$page,$size);
        $total = $this->userModel->getCount($where);
        if(!$userList) {
            return returnJson(0,'',[
                'list' => [],
                'size' => $size,
                'total' => $total
            ]);
        }
        foreach($userList as $k=>$v) {
            if($v['refer_id']) {
                $userList[$k]['refer_info'] = $this->userModel->getOne([
                    'id' => $v['refer_id']
                ],'nickname,id');
            }
            $userList[$k]['grade_name'] = GradeModel::id2name($v['grade_id']);
            $userList[$k]['cash'] *= config('dikou_rate');
            $userList[$k]['mall_name'] = MallModel::where('id',$v['mall_id'])->value('name');
        }
        return returnJson(0,'',[
            'list' => $userList,
            'size' => $size,
            'total' => $total
        ]);
    }

    public function edit () {
        if(request()->isPost()) {
            $input = input();
            if(!isset($input['id'])) {
                return returnJson(1,'请填写完整');
            }
            $userInfo = UserModel::getOneById($input['id']);

            $data = array(
                'id' => $input['id'],
                'realname' => $input['realname'],
                'mobile' => $input['mobile'],
                'refer_id' => $input['refer_id'],
                'tag' => $input['tag'],
                'chat_type' => $input['chat_type'],
                'chat_forbidden' => $input['chat_forbidden'],
                //'grade_id' => $input['grade_id']
            );
            if($input['refer_id'] && UserModel::checkRefer($input['id'],$input['refer_id'])==false) {
                return returnJson(1,'推荐人设置错误');
            }
            $res = $this->userModel->where(['id' => $data['id']])->update($data);
            if($res || $userInfo['grade_id']!=$input['grade_id']) {
                if($userInfo['grade_id']!=$input['grade_id']) {
                    //限制分公司修改等级
                   /* if($this->manager['mall_id']!=0 && $input['grade_id']==3) {
                        return returnJson(1,'无权修改');
                    }*/
                    //记录
                    GradeModel::changeGrade($input['id'],$userInfo['grade_id'],$input['grade_id'],$this->manager['name'].'修改');
                }
                if($userInfo['refer_id']!=$input['refer_id']) {
                    //记录
                    ActionLogModel::create([
                        'uid' => $input['id'],
                        'type' => 2,
                        'note' => $this->manager['name'].'修改',
                        'old' => $userInfo['refer_id'],
                        'new' => $input['refer_id']
                    ]);
                }
                return returnJson(0,'操作成功');
            }
            return returnJson(1,'无任何修改');
        }
    }

    /* 分公司修改用户信息 */
    public function malledit () {
        if(request()->isPost()) {
            $input = input();
            if(!isset($input['id'])) {
                return returnJson(1,'请填写完整');
            }
            $userInfo = UserModel::getOneById($input['id']);

            $data = array(
                'id' => $input['id'],
                'realname' => $input['realname'],
                'mobile' => $input['mobile'],
                'refer_id' => $input['refer_id'],
                'tag' => $input['tag'],
                'chat_type' => $input['chat_type'],
                //'grade_id' => $input['grade_id']
            );
            if($input['refer_id'] && UserModel::checkRefer($input['id'],$input['refer_id'])==false) {
                return returnJson(1,'推荐人设置错误');
            }
            $res = $this->userModel->where(['id' => $data['id']])->update($data);
            if($res) {
                return returnJson(0,'操作成功');
            }
            return returnJson(1,'网络异常，请重试');
        }
    }



    /* 用户等级列表 */
    public function gradeList () {
        /*if($this->manager['mall_id']==0) {
            $where = 1;
        }else{
            $where = [
                'id' => ['in',[1,2,4]]
            ];
        }*/
        $where=1;
        $list = GradeModel::getAll($where,'id asc');
        return returnJson(0,'success',$list);
    }


    /* 修改等级 */
    public function gradeEdit () {
        $input = input();
        GradeModel::where(['id'=>$input['id']])->update([
            'grade_name' => $input['grade_name']
        ]);
        return returnJson(0,'保存成功');
    }
}
